#!/usr/bin/env python

# copyright 2010 Studio Cypher, LLC
# developer: Will Emigh
# projects@studiocypher.com

# Custom modules
from authentication import user_required
from authentication import get_account_from_user
from models import Account
from models import TradeRecord
from models import MessageRecord
from models import Colony
from models import ColonyShip
from models import RecyclingShip
from models import FreighterShip
from models import UnitType
from models import Log

# Built-in or Google modules
import cgi
import os
#import imghdr
from google.appengine.ext.webapp import template
from google.appengine.api import images
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import users
from google.appengine.ext import db
  
class ViewMessage(webapp.RequestHandler):
    def get(self):
        messageKey = self.request.get("message_key")
        thisMessage = MessageRecord.get(db.Key(messageKey))

        if thisMessage:        

            template_values = {
            'message': thisMessage,
            }

            path = os.path.join(os.path.dirname(__file__), 'message_receive.html')
            self.response.out.write(template.render(path, template_values))
        
        
  
class DeleteMessage(webapp.RequestHandler):
    def get(self):
        messageKey = self.request.get("message_key")
        thisMessage = MessageRecord.get(db.Key(messageKey))

        thisMessage.delete()

        self.redirect("/profiler/private?res_type=5")
        
        
  

class CreateMessage(webapp.RequestHandler):
    def post(self):
    
        user = users.get_current_user()
        ownerAccount = get_account_from_user(user)
        
        accountKey = self.request.get("recip_key")
        recipAccount = Account.get(db.Key(accountKey))

        content = self.request.get("content")
        
        message = MessageRecord(owner=ownerAccount, content=content, to=str(recipAccount.key()))
        
        message.put()
        
        self.redirect("/profiler/private?res_type=5")
        

  

class SendMessage(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()
        ownerAccount = get_account_from_user(user)
        
        accountKey = self.request.get("recip_key")
        recipAccount = Account.get(db.Key(accountKey))
            
        if recipAccount:        

            template_values = {
            'ownerAccount': ownerAccount,
            'recipAccount': recipAccount,
            }

            path = os.path.join(os.path.dirname(__file__), 'message.html')
            self.response.out.write(template.render(path, template_values))
        


  

class ViewMessage(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()
        ownerAccount = get_account_from_user(user)
        
        messageKey = self.request.get("message_key")
        myMessageRecord = MessageRecord.get(db.Key(messageKey))
            
        template_values = {
            'message': myMessageRecord,
            }

        path = os.path.join(os.path.dirname(__file__), 'message_receive.html')
        self.response.out.write(template.render(path, template_values))
        

  
  
  
  
  
  
  
  
  
  
  
  

application = webapp.WSGIApplication(
                                     [('/message/create_message', CreateMessage),
                                     ('/message/delete_message', DeleteMessage),
                                     ('/message/view_message', ViewMessage),
                                     ('/message/send_message', SendMessage),],
                                     debug=True)									 
def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
